//===================================
//File: List.h
//List definition
//Name: Smirnova O.P.
//Date: 11.07.11, v.1.0
//===================================
#ifndef LIST_H
#define LIST_H
#include "Elem.h"
template <class T>
class List{
private:
	Elem<T>* head;
public:
	List(Elem<T>* head)
	{
	  this->head = head;
	}
	Elem<T>* getHead()
	{
		return head;
	}
	void setHead(Elem<T>* head)
	{
		this->head = head;
	}
	short isEmpty()
	{
	  if(head==0) return 1; else return 0;
	}
	int size()
	{
	  if(isEmpty()==1) return 0;
	  else
	  {
	    Elem<T> *temp = head;
	    int i=0;
	    do
	    {
	      head = head->getNext();
	      i++;
	    }
	    while(head!=temp);
		head = temp;
	    return i;
	   }
	}
	void addElemToBegin(T e)
	  {
		  Elem<T>* temp = new Elem<T>(e);

		  if(head==NULL)
		  {
			  temp->setNext(temp);
		  }
		  else
		  {
			  temp->setNext(head);
			  Elem<T>* iter = head;
			  while(iter->getNext()!=head) iter = iter->getNext();
			  iter->setNext(temp);
		  }
		  head = temp;


	  }
	void addElemToEnd(T e)
	 {
		  Elem<T>* temp = head;
		  if(isEmpty())
		  {
			  head = new Elem<T>(e);
			  head->setNext(head);
		  }
		  else
		  {
			  while(head->getNext()!=temp)
			  {
				  head = head->getNext();
			  }
			  Elem<T> *t = new Elem<T>(e);
			  t->setNext(head->getNext());
			  head->setNext(t);
			  head = temp;
		  }

	  }
	void writeList()
	  {
		  Elem<T> *temp = head;
		  do
		  {
			  std::cout << head->getValue() << std::endl;
			  head = head->getNext();
		  }while(head!=temp);
		  head = temp;
	  }


};
#endif





